#!/bin/bash

function calc_counters()
{
	if [ -z "$2" ]
	then
		echo "-Parameter #2 is zero length.-"
		exit -2
	fi
	
	#calculate A1-A$2, B1-B$2 Rx counters
	echo -e "Port\t\t\tRx Good Pkts" 
	i=1
	sum_ab=0
	while [ $i -le $2 ]
	do
		A[$i]=$(cat $1 |grep -A1 "A$i]" | awk '{print $4}')
		
		OVERSIZE_A[$i]=$(cat $1 | grep -A3 "A$i]" | grep "Oversize" | awk '{print $4}')
		ALIGNMENT_A[$i]=$(cat $1 | grep -A4 "A$i]" | grep "Alignment" | awk '{print $4}')
		FCS_A[$i]=$(cat $1 | grep -A5 "A$i]" | grep "FCS" | awk '{print $4}')
		FRAGMENT_A[$i]=$(cat $1 | grep -A6 "A$i]" | grep "Fragment" | awk '{ print $3}')
		
		B[$i]=$(cat $1 |grep -A1 "B$i]" | awk '{print $4}')
		
		OVERSIZE_B[$i]=$(cat $1 | grep -A3 "B$i]" | grep "Oversize" | awk '{print $4}')
		ALIGNMENT_B[$i]=$(cat $1 | grep -A4 "B$i]" | grep "Alignment" | awk '{print $4}')
		FCS_B[$i]=$(cat $1 | grep -A5 "B$i]" | grep "FCS" | awk '{print $4}')
		FRAGMENT_B[$i]=$(cat $1 | grep -A6 "B$i]" | grep "Fragment" | awk '{ print $3}')
		#echo ${OVERSIZE_A[$i]} 
		
		if [ ${OVERSIZE_A[$i]} -ne 0 ] || [ ${ALIGNMENT_A[$i]} -ne 0 ] || \
			[ ${FCS_A[$i]} -ne 0 ] || [ ${FRAGMENT_A[$i]} -ne 0 ]
		then
			echo Warning!!!!!!!Port A[$i] have error packet, result may not be correct.
		elif [ ${OVERSIZE_B[$i]} -ne 0 ] || [ ${ALIGNMENT_B[$i]} -ne 0 ] || \
			[ ${FCS_B[$i]} -ne 0 ] || [ ${FRAGMENT_B[$i]} -ne 0 ] 
		then
			echo Warning!!!!!!!Port B[$i] have error packet, result may not be correct.
		fi
		echo -e A$i "\t\t\t"${A[$i]}
		echo -e B$i "\t\t\t"${B[$i]}
		
		sum_ab=`expr ${A[$i]} + ${B[$i]} + $sum_ab`
		let i+=1
	done
	#check error packet counter
	
	
	#calculate C1-C8 Tx counters 
	echo -e "\nPort\t\t\tTx Pkts" 
	j=1
	while [ $j -le 8 ]
	do
		C[j]=$(cat $1 |grep -A7 "C$j]" | awk '{print $4}' | sed -n "2p")
		echo -e C$j "\t\t\t"${C[$j]}
		let j+=1
	done
	echo -e "\n"Port A[1]...B[$2] counters sum is: $sum_ab	
	if [ $sum_ab -eq ${C[1]} ] && [ $sum_ab -eq ${C[2]} ] && \
		[ $sum_ab -eq ${C[3]} ] && [ $sum_ab -eq ${C[4]} ] && \
		[ $sum_ab -eq ${C[5]} ] && [ $sum_ab -eq ${C[6]} ] && \
		[ $sum_ab -eq ${C[7]} ] && [ $sum_ab -eq ${C[8]} ]
	then
		echo -e "\n****************Congratulations****************"
		echo -e "Port A,B's counter is equal to Port C's counter\n"
	else
		echo -e "\n*********************FAILED!!!*********************"
		echo -e "Port A,B's counter is NOT EQUAL TO Port C's counter\n"	
	fi
	#echo -e "\n************************************************"
	echo -e "File of $logs analysis end\n\n\n\n"
	#echo -e "************************************************\n\n\n\n"
}


for logs in `ls *_expect.log`
do
	TYPE=$(cat $logs | grep "EmbedWay ExTAP" | awk '{ print $2 }' | tr -d "[:space:]")
	SYSIP=$(cat $logs | grep "System ip/mask addr" | awk 'BEGIN {OFS="/"} {print $4}' | tr -d "[:space:]")
	
	echo -e "\n************************************************"
	echo -e "File of $logs analysis begin..."
	echo -e "************************************************"

	echo -e "Board	Type: 		$TYPE"
	echo -e "System	IP:     	$SYSIP"
	if [ "$TYPE" == "ExTAP2180" ]
	then
		calc_counters $logs 8
		
	elif [ "$TYPE" == "ExTAP2280" ]
	then
		calc_counters $logs 16
	elif [ "$TYPE" == "ExTAP2480" ]
	then
		calc_counters $logs 24
	else	
		echo "Unknown Board TYPE"
	fi
done
